package cash.cmp.personal_financial_system.bigdata;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class ExecuteJob {
    @Autowired
    private RestTemplate restTemplate;

    public String executeFlow(String sessionID, String project, String flow, String dt) {
        String url = "http://150.158.2.228:8081" + "/executor?ajax=executeFlow";
        HttpHeaders httpHeaders = new HttpHeaders();
        LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<>();
        linkedMultiValueMap.add("session.id", sessionID);
        linkedMultiValueMap.add("project", project);
        linkedMultiValueMap.add("flow", flow);
        linkedMultiValueMap.add("flowOverride[useExecutor]", "1");
        linkedMultiValueMap.add("flowOverride[dt]", dt);
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(linkedMultiValueMap, httpHeaders);
        ResponseEntity<String> result = restTemplate.postForEntity(url, httpEntity, String.class);
        System.out.println(result.getBody());
        return result.getBody();
    }
}
